package com.toolset.Activity.ToolItemActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.toolset.Activity.BaseActivity;
import com.toolset.Activity.MainActivity;
import com.toolset.Bean.ToolItem;
import com.toolset.R;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import layout.HomeFragment;
import layout.UserFragment;

import static android.R.attr.data;
import static android.R.attr.updatePeriodMillis;

/*
备忘录
 */
public class Memo extends BaseActivity{
    private EditText memotext;    //输入文本
    private Button memobtn;        //保存按钮
    ArrayAdapter<String> adapter;   //适配器
    ListView listView;              //listView控件;
    List <String>list=new ArrayList(); //定义list数组


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memo);
        initEven();
        listView.setAdapter(adapter);//关联适配器

        //拿出数据

        read();

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {

    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {


    }
});
        memobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入文本，并存入list集合
                String content = memotext.getText().toString();
                list.add(content);
                adapter.clear();//因为把文本都放入数组，需要清楚一次，才能避免重复
                adapter.addAll(list);
                write();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, final View view, final int position, long l) {
                Toast.makeText(Memo.this,list.get(position),Toast.LENGTH_LONG).show();
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {

                String k=list.get(position);
                list.remove(position);
                adapter.remove(k);
                write();
                return true;
            }
        });


    }

    private void read() {
        SharedPreferences pref = getSharedPreferences("text", MODE_PRIVATE);
        String json = pref.getString("jsonText", "");
        if (json.length()!=0) {
            Gson gson = new Gson();
            list = gson.fromJson(json, List.class);
            adapter.addAll(list);
        }
    }

    private void write(){  //json是一种数据格式，便于数据传输、存储、交换。gson是一种组件库，可以把java对象数据转换成json数据格式。
        Gson gson = new Gson();
        String json = gson.toJson(list);//java对象转json输出的结果
        SharedPreferences.Editor editor = getSharedPreferences("text", MODE_PRIVATE).edit();
        editor.putString("jsonText", json);
        editor.apply();
    }



    private void initEven() {
        memotext= (EditText) findViewById(R.id.memo_text);
        memobtn= (Button) findViewById(R.id.memo_btn);
        adapter = new ArrayAdapter<String>(Memo.this,android.R.layout.simple_list_item_1);
        listView = (ListView)findViewById(R.id.list_item);
    }


}
